/**
*存放百度地图的js接口，包括定位、浏览等功能
**/
var deviceone = require("deviceone");
var nf = deviceone.sm("do_Notification");
var app = deviceone.sm("do_App");
var dingwei = deviceone.sm("do_BaiduLocation");
var jpush = deviceone.sm("do_JPush");
var httpUtil = require("helper/httpSilence");
var userUrl = require("url/user");
var sysUrl = require("url/sys");
var deviceUtil = require("helper/device");
var qqCacher = require("helper/qqCacher");
var indexCacher = require("helper/indexCacher");
var defaultDingwei = {"longitude":"114.064518","latitude":"22.548607","address":"广东省深圳市","cityName":"深圳市"};
var startPoint = "22.548607,114.064518";
//打开app开始定位
module.exports.initDingwei = function(){
	dingwei.start({model:"high",isLoop:true});
};

//获取打开app时的定位
module.exports.getDingwei = function(){
	return qqCacher.currentLocation();
};
dingwei.on("result",function(data,e){
    if(data.latitude != "4.9E-324"){
		dingwei.reverseGeoCode(data.latitude,data.longitude,function(data2, e2){
			var provinceName = data2.province;
			var cityName = data2.city;
			//nf.alert("provinceName-"+provinceName+"==="+"cityName-"+cityName);
			data.cityName = cityName;
			qqCacher.currentLocation("");
			qqCacher.currentLocation(JSON.stringify(data));
			var currentLocationName = indexCacher.currentLocationName();
			var newCurrentLocationName = provinceName+"_"+cityName;
			if(currentLocationName!=newCurrentLocationName){
				indexCacher.currentLocationName(newCurrentLocationName);
				httpUtil.post(sysUrl.url.area.getAreaByNameAll,{"provinceName":provinceName,"nameAll":cityName},function(data4){
					var area = data4.area;
					if(area){
						indexCacher.currentCityName(area.name);
						indexCacher.currentCityId(area.id);
					}
				});
			}
		});
		startPoint = qqCacher.currentStartPoint();
		var endPoint = ""+data.latitude+","+data.longitude;
		var distance = dingwei.getDistance({"startPoint":startPoint,"endPoint":endPoint})
		if(distance>=5){
			var info = deviceUtil.getInfo();
		    if(info){
		    	data.x = data.longitude;
		    	data.y = data.latitude;
		    	data.deviceId = info.deviceId;
		    	data.devicePhone = info.phoneNumber;
		    	data.contentVersion = qqCacher.currentContentVersion();
		    	data.nativeVersion = qqCacher.currentNativeVersion();
		    	data.platformCodeStr = deviceUtil.getOsType();
		    	data.registrationId = jpush.getRegistrationID();
		    	qqCacher.currentStartPoint("");
		    	qqCacher.currentStartPoint(endPoint);
		    	httpUtil.post(userUrl.url.spoor.add,data,function(data3){});
		    }
		}
    }else {
    	qqCacher.currentLocation("");
    	qqCacher.currentLocation(JSON.stringify(defaultDingwei));
	}
});